﻿using LJ.AspNetCore.Abstractions.Pack;
using LJ.WebApi.Mvc.DynamicWebApi;
using LJ.WebApi.UnifyResult.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace LJ.WebApi.UnifyResult.Mvc.Extensions;

public static class AspNetCorePackContextExtensions
{
    /// <summary>
    /// 统一处理
    /// </summary>
    /// <param name="packContext"></param>
    /// <returns></returns>
    public static IAspNetCorePackContext AddUnifyData(this IAspNetCorePackContext packContext)
    {
        //禁用模型验证过滤器
        packContext.DependencyManager.ServiceCollection.Configure<ApiBehaviorOptions>(options =>
        {
            options.SuppressModelStateInvalidFilter = true;
            options.SuppressMapClientErrors = false;
        });

        packContext.DependencyManager.ServiceCollection.AddOptions<MvcOptions>()
         .Configure<IServiceProvider>((options, serviceProvider) =>
         {
             options.Filters.Add<UnifyExceptionFilter>();
             options.Filters.Add<UnifyResultFilter>();
             options.Filters.Add<UnifyValidationFilter>();
         });

        packContext.DependencyManager.ServiceCollection.AddTransient<IConfigureAction, UnifyResultConfigureAction>();

        return packContext;
    }
}
